﻿using hutil;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Configuration;
namespace hlog {
    /**
   *┌────────────────────────────────────────────────┐
   *│　描    述：CommonCache                                                    
   *│　作    者：sgj                                              
   *│　版    本：1.0                                              
   *│　创建时间：2020/9/16 11:22:25                        
   *└────────────────────────────────────────────────┘
   **/
    public  class ConnectionCache {
         protected static MongoServer mongoServer;
        private static Dictionary<string ,MongoCollection> dicts = new Dictionary<string ,MongoCollection>();
        private static String DB_NAME = "";

 
        static ConnectionCache() {
            ConnectionStringSettings connectionStringSettings = ConfigurationManager.ConnectionStrings["MongoDB"];
            if( connectionStringSettings == null ) {
                return;
            }
             DB_NAME = connectionStringSettings.ProviderName;
            if( String.IsNullOrWhiteSpace(DB_NAME) ) {
                DB_NAME = "logger";
            }
             MongoClientSettings settings = new MongoClientSettings();
            settings.WaitQueueSize = int.MaxValue;
            settings.WaitQueueTimeout = new TimeSpan(0 ,2 ,0);
            settings.MinConnectionPoolSize = 10;
            settings.MaxConnectionPoolSize = 300;
            settings.MaxConnectionIdleTime = new TimeSpan(1 ,2 ,0);
            settings.MaxConnectionLifeTime = new TimeSpan(1 ,2 ,0);
             settings.Server = new MongoServerAddress(connectionStringSettings.ConnectionString);
            settings.SocketTimeout = new TimeSpan(0 ,1 ,0);
            var client = new MongoClient(settings);
            mongoServer = client.GetServer();

           


        }
        
  
        public static MongoCollection<BsonDocument> getMongoCollection<T>() {
            
            Type type = typeof(T);
            string name = HUtil.GetTableName(type);
            return createMongoCollection(name);
            
            
        }
         public static MongoCollection<BsonDocument> getMongoCollection(String name) {

            return createMongoCollection(name);
           
           
        }
        public static MongoCollection<BsonDocument> getMongoCollection(Type type) {
            String name = HUtil.GetTableName(type);
            return createMongoCollection(name);
          
        }
        private static MongoCollection<BsonDocument> createMongoCollection(string name)
        {
            if( mongoServer == null ) {
                return null;
            }
            lock (dicts)
            {
                 if (!dicts.ContainsKey(name) )
                {
                    if( dicts.Count > 500 ) {
                        dicts.Clear();
                    }
                    MongoDatabase database = mongoServer.GetDatabase(DB_NAME);
                    MongoCollection collection = database.GetCollection(name);
              
                    dicts.Add(name , collection);
                }
              
            }

            if( dicts.ContainsKey(name) ) {
                return ( MongoCollection<BsonDocument> ) dicts[name];
            }
            return null;
        }

        public static bool hasCollection(String name) {
            MongoDatabase database = mongoServer.GetDatabase(DB_NAME);
            IEnumerable<string> it= database.GetCollectionNames();
            foreach( String i in it ) {
                if( i == name ) {
                    return true;
                }
            }
            return false;

        }
    }
}
